from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from users.models import User


class UserListSerializer(ModelSerializer):

    class Meta:
        model = User
        fields = ('id','username','mobile','email')

class UserAddSerializer(ModelSerializer):

    class Meta:
        model = User
        fields = ('id','username','mobile','email','password')
        extra_kwargs = {
            'username': {'min_length': 5, 'max_length': 20,'required': True},
            'password': {'min_length': 8, 'max_length': 20,'write_only': True},
        }
    def create(self, validated_data):
        # 创建普通用户，密码使用明文显示
        user = User.objects.create_user(**validated_data)
        return user
